package com.michaelbulava.TFS.ui.controls.connect;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import com.michaelbulava.TFS.Messages;
import com.michaelbulava.TFS.ui.controls.generic.JWrappingLabel;
import com.michaelbulava.TFS.ui.helpers.SystemIconHelper;
import com.microsoft.tfs.core.ws.runtime.exceptions.UnauthorizedException;

import javax.swing.*;
import java.awt.*;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.text.MessageFormat;

/**
 * Created by mbulava on 6/15/2015.
 */
public class ConnectionErrorControl extends JPanel {
    private JLabel imageLabel;
    private JWrappingLabel messageLabel;

    private URI serverURI;
    private Exception failure;
    private String message;
    private int iconId = 8;
    private Icon icon;

    public ConnectionErrorControl(){
        super();
        setupUI();
        computeMessage();
    }
    private void setupUI(){
        setLayout(new GridLayoutManager(2, 2, new Insets(0, 0, 0, 0), -1, -1));
        imageLabel = new JLabel();
        imageLabel.setText("");
        add(imageLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTH, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final Spacer spacer1 = new Spacer();
        add(spacer1, new GridConstraints(1, 0, 1, 2, GridConstraints.ANCHOR_CENTER,
                GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        messageLabel= new JWrappingLabel();
        messageLabel.setText("");
        add(messageLabel, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_NORTH, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
    }

    public void setServerURI(URI serverURI){
        this.serverURI = serverURI;
        computeMessage();
    }

    public void setException(Exception failure){
        this.failure = failure;
        computeMessage();;
    }

    public Exception getException(){
        return this.failure;
    }

    public void setMessage(String message){
        this.message = message;
        computeMessage();;
    }

    public String getMessage(){
        return this.message;
    }

    public void setIconId(int iconId){
        this.iconId = iconId;
        computeMessage();
    }

    public int getIconId(){
        return this.iconId;
    }

    private void computeMessage(){
        this.icon = SystemIconHelper.getSystemIcon(this.iconId);
        this.imageLabel.setIcon(this.icon);

        StringBuffer fullMsg = new StringBuffer();
        if (this.failure != null){
            String headerMessage;
            if (this.serverURI == null) {
                headerMessage = Messages.getString("ConnectionErrorControl.CouldNotConnectTfs");
            } else {
                headerMessage = MessageFormat.format(Messages.getString("ConnectionErrorControl.CouldNotConnectTfsWithServerURIFormat"), new Object[]{this.serverURI.toString()});
            }
            fullMsg.append(headerMessage);
            if (getExceptionMessage() != null)
            {
                fullMsg.append("\n");
                fullMsg.append(getExceptionMessage());
                fullMsg.append(".");
            }
            if (this.message != null) {
                fullMsg.append("\n\n");
            }
        }
        if (this.message != null) {
            fullMsg.append(this.message);
        }
        this.messageLabel.setText(fullMsg.toString());
    }

    private String getExceptionMessage(){
        if (this.failure == null)
            return null;
        if (this.failure instanceof UnauthorizedException)
            return Messages.getString("ConnectionErrorControl.UserNameNotAccepted");
        if (this.failure instanceof SocketTimeoutException)
            return Messages.getString("ConnectionErrorControl.ConnectionTimedOut");

        return this.failure.getMessage();
    }
}

